package com.eleadmin.order.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.CustomerFactoryBiz;
import com.eleadmin.order.entity.CustomerFactory;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2023-03-01 16:34:53
 * @version ：1.0
 */
@Repository
public class CustomerFactoryBizImpl extends BaseDao implements CustomerFactoryBiz {

    @Override
    public JsonResult list(PageResult2<T> page, CustomerFactory customerFactory, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<CustomerFactory> rm = new BeanPropertyRowMapper<CustomerFactory>(CustomerFactory.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.faddress,c.pinyin,c.fstate");
		sql.append(",c.longitude,c.latitude,c.radius,c.fremark");
		sql.append(",c.create_time,c.create_user,c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_customer_factory c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=c.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 and c.customer_id=:customer_id");
        map.put("customer_id", customerFactory.getCustomerId()+"");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(customerFactory)){
            if(Utils.isNotEmpty(customerFactory.getFaddress())){
                sql.append(" and instr(c.faddress,:faddress)>0 ");
                map.put("faddress", customerFactory.getFaddress());
            }
          if(Utils.isNotEmpty(customerFactory.getSort())&&Utils.isNotEmpty(customerFactory.getOrder())){
              sql.append(" order by c."+customerFactory.getSort()+" "+customerFactory.getOrder());
          } else {
              sql.append(" order by c.create_time desc");
          }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<CustomerFactory> list(CustomerFactory customerFactory, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<CustomerFactory> rm = new BeanPropertyRowMapper<CustomerFactory>(CustomerFactory.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.faddress,c.pinyin,c.fstate");
		sql.append(",c.longitude,c.latitude,c.radius,c.fremark");
		sql.append(",c.create_time,c.create_user,c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_customer_factory c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 and c.customer_id=:customer_id");
        map.put("customer_id", customerFactory.getCustomerId()+"");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(customerFactory)){
            if(Utils.isNotEmpty(customerFactory.getSort())&&Utils.isNotEmpty(customerFactory.getOrder())){
                sql.append(" order by c."+customerFactory.getSort()+" "+customerFactory.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public CustomerFactory getById(String id) {
        RowMapper<CustomerFactory> rm = new BeanPropertyRowMapper<CustomerFactory>(CustomerFactory.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.faddress,c.pinyin,c.fstate");
		sql.append(",c.longitude,c.latitude,c.radius,c.fremark");
		sql.append(",c.create_time,c.create_user,c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_customer_factory c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 and c.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(CustomerFactory customerFactory, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into chf_customer_factory (");
        sql.append("organization_id,customer_id,faddress,pinyin");
		sql.append(",fstate,longitude,latitude");
		sql.append(",radius,fremark,create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:faddress,:pinyin");
		sql.append(",:fstate,:longitude,:latitude");
		sql.append(",:radius,:fremark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", customerFactory.getCustomerId()==null?null:customerFactory.getCustomerId()+"");
        map.put("faddress", customerFactory.getFaddress());
        map.put("pinyin", customerFactory.getPinyin());
        map.put("fstate", customerFactory.getFstate());
        map.put("longitude", customerFactory.getLongitude());
        map.put("latitude", customerFactory.getLatitude());
        map.put("radius", customerFactory.getRadius()==null?null:customerFactory.getRadius()+"");
        map.put("fremark", customerFactory.getFremark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(CustomerFactory customerFactory, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update chf_customer_factory set ");
        sql.append("faddress=:faddress,pinyin=:pinyin ");
		sql.append(",fstate=:fstate,longitude=:longitude ");
		sql.append(",latitude=:latitude,radius=:radius,fremark=:fremark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("faddress", customerFactory.getFaddress());
        map.put("pinyin", customerFactory.getPinyin());
        map.put("fstate", customerFactory.getFstate());
        map.put("longitude", customerFactory.getLongitude());
        map.put("latitude", customerFactory.getLatitude());
        map.put("radius", customerFactory.getRadius()==null?null:customerFactory.getRadius()+"");
        map.put("fremark", customerFactory.getFremark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", customerFactory.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean updateFstate(CustomerFactory customerFactory, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update chf_customer_factory set ");
		sql.append("fstate=''");
		sql.append(",update_time=now(),update_user=:update_user where customer_id=:customer_id ");
        map.put("customer_id", customerFactory.getCustomerId()==null?null:customerFactory.getCustomerId()+"");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", customerFactory.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(id.length()>0){
            for(String id1 : id.split(",")){
                map  = new HashMap<String, String>();
                if(id1==null || id1.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update chf_customer_factory set deleted=1" +
                    ",update_user=:update_user, update_time=now() "
                    + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}
